import { defineStore } from "pinia";
import {
    reqGetSwiper,
    reqGetRecommend,
    reqGetInterestList,
    reqGetSlectedInterest,
    reqGetSearchInterest,
} from "@/api/index/index";
import type { swiperListType, recommendListType, tagListType, searchList, tagType } from "@/api/index/interface";

interface indexStateType {
    swiperList: swiperListType;
    recommendList: recommendListType;
    tagList: tagListType;
    searchInterestList: searchList;
    interesTagList: searchList;
}

export const useIndexStore = defineStore({
    id: "indexData",
    state: (): indexStateType => {
        return {
            swiperList: [],
            recommendList: [],
            tagList: [],
            searchInterestList: [],
            interesTagList: [],
        };
    },
    actions: {
        // 获取轮播图
        async getSwiperListAsync() {
            const re = await reqGetSwiper();
            this.swiperList = re.content;
        },
        // 获取推荐数据
        async getRecommendAsync() {
            const re = await reqGetRecommend();
            this.recommendList = re.content;
        },
        // 获取全部兴趣列表
        async getInterestListAsync(pageNum: number, pageSize: number) {
            const re = await reqGetInterestList();
            this.tagList = re.content;
        },
        // 获取加载兴趣列表
        async getNewInterestListAsync(pageNum: number, pageSize: number) {
            const re = await reqGetInterestList(pageNum, pageSize);
            re.content.forEach((item) => this.tagList.push(item));
        },
        // 获取已选兴趣列表
        async getSlectedInterestAsync() {
            const re = await reqGetSlectedInterest();
        },
        // 搜索关键字
        async getSearchInterest(searchValue: string) {
            const re = await reqGetSearchInterest(searchValue);
            this.searchInterestList = re.content;
        },
    },
});
